#include "calendaralgorithm.h"

CalendarAlgorithm::CalendarAlgorithm()
{

}

// 算法博客：http://blog.csdn.net/orbit/article/details/7749723
int CalendarAlgorithm::getDayOfWeek(int nYear, int nMonth, int nDay)
{
    int m = nMonth;
    int d = nDay;
    if(nMonth <= 2)
    {
        nYear --;
        m = nMonth + 12;
    }

    int y = nYear % 100;
    int c = nYear / 100;
    int w = (y + y / 4 + c / 4 - 2 * c + (13 * (m + 1) / 5) + d - 1) % 7;

     if(w < 0) /*修正计算结果是负数的情况*/
        w += 7;
     return w;
}

int CalendarAlgorithm::getDaysOfMonth(int nYear, int nMonth)
{
    QDate d(nYear, nMonth, 1);
    QDate dTemp = d.addDays(28);
    if(dTemp.month() != d.month())
        return 28;
    dTemp = d.addDays(29);
    if(dTemp.month() != d.month())
        return 29;
    dTemp = d.addDays(30);
    if(dTemp.month() != d.month())
        return 30;
    return 31;
}
